home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSDRIVE.C < prev    next >
C/C++ Source or Header  |  1995-08-04  |  12KB  |  453 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bsdrive.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains .
  25. //
  26. //    The code in this module should be written entirely in C. 
  27. //    Do not use any C++ constructs.
  28. //
  29. //    This module is portable to:
  30. //        DOS 3.X+
  31. //        MS Windows 3.X+
  32. //        OS/2 2.X+
  33. //        OS/2 2.0 PM
  34. //        SCO UNIX.
  35. //
  36. //    The following compilers are supported:
  37. //        MSC 6.0A
  38. //        MSC/C++ 7.0
  39. //        Borland C++ 3.1 for DOS
  40. //        Borland C++ 1.0 for OS/2 2.X
  41. //        SCO UNIX cc
  42. //
  43. //----------------------------------------------------------------------------
  44. #include <bs.h>
  45.  
  46. //----------------------------------------------------------------------------
  47. //    Globals
  48. //----------------------------------------------------------------------------
  49. #define DOS_DATA_SEG  0x0050
  50. #if OS_DOS || OS_WINDOWS
  51. static SHORT FN_L DriveGetLogical(SHORT);
  52. #endif
  53. static SHORT FN_L DriveSetLogical(SHORT);
  54.  
  55.  
  56. //----------------------------------------------------------------------------
  57. //   Description:    Query current drive
  58. //    Parameters:    
  59. //       Returns:    Current drive. 1=A, 2=B, ...
  60. //----------------------------------------------------------------------------
  61. SHORT FN_E DriveGet(void)
  62. {
  63. #if OS_UNIX
  64.     return 0;
  65. #else
  66.     unsigned drive;
  67.     _dos_getdrive(&drive);
  68.    return (SHORT)drive;
  69. #endif
  70. }
  71.  
  72.  
  73. //----------------------------------------------------------------------------
  74. //   Description:    Get logical drive for current drive.
  75. //    Parameters:    sDrive        Drive letter. 1..26
  76. //       Returns:    Previous logical drive.
  77. //----------------------------------------------------------------------------
  78. #if OS_DOS || OS_WINDOWS
  79. static SHORT FN_L DriveGetLogical(SHORT sDrive)
  80. {
  81.     SHORT sCurDrive;                           // Current drive attached to block device
  82.     union REGS regs;                           // Registers for interrupt calls.
  83.  
  84.    if (_osmajor > 3 || _osmajor == 3 && _osminor >= 20)
  85.       {
  86.       // IOCTL request 0x0E (get logical drive). 
  87.       regs.x.ax = 0x440E;
  88.       // Drive number (1 = A, 2 = B, etc) is in BL. 
  89.       regs.h.bl = (UCHAR)sDrive;
  90.        intdos(®s, ®s);
  91.       if (regs.x.cflag)                        // Carry flag set, error in _doserrno. 
  92.           sCurDrive = 0;                        // Return code of 0 in AL means only 
  93.       else                                        // one drive attachedto block device. 
  94.           sCurDrive = regs.h.al == 0 ? sDrive : regs.h.al + ('A' - 1);
  95.         }
  96.    else if (sDrive == 1 || sDrive == 2)// For lower versions of DOS, only drives
  97.       {                                            // A and B are interchangeable. 
  98.        int86(0x11, ®s, ®s);
  99.        // Bit 0 in AX is 1 if diskette drives are present. 
  100.       if (regs.x.ax & 0x0001)
  101.             {                                        // Only one diskette installed.
  102.           if (((regs.x.ax & 0xC0) >> 6) == 0)
  103.                 {
  104. //            sCurDrive = sDrive == 1 || sDrive == 2 ?
  105. //            peekb(DOS_DATA_SEG, 0x0004) + 1 : sDrive;
  106.                 }
  107.          else
  108.             sCurDrive = sDrive;            // More than one diskette installed.
  109.             }
  110.        else                                        // No diskette drive.
  111.          sCurDrive = 0;
  112.        }
  113.    else
  114.         sCurDrive = sDrive;
  115.  
  116.     return sCurDrive;
  117. }
  118. #endif
  119.  
  120.  
  121. //----------------------------------------------------------------------------
  122. //   Description:    Determine if a drive is remote
  123. //    Parameters:    sDrive    Drive may be 1..26 (A..Z), A..Z, or a..z.
  124. //                                    Drive is converted to a number 1..26.
  125. //       Returns:    TRUE if drive is valid and remote
  126. //----------------------------------------------------------------------------
  127. BOOL FN_E DriveIsRemote(SHORT sDrive)
  128. {
  129. #if OS_UNIX
  130.  
  131.     NOTUSED(sDrive);
  132.     return FALSE;
  133.  
  134. #else
  135.  
  136.     BOOL fResult = FALSE;
  137.  
  138.     if (!DriveIsValid(sDrive, &sDrive))
  139.         return FALSE;
  140.  
  141. #if OS_DOS || OS_WINDOWS
  142.  
  143.    if (_osmajor > 3 || _osmajor == 3 && _osminor >= 20)
  144.         {
  145.        _asm {
  146.            mov ax, 4409h
  147.            mov bx, [sDrive]
  148.            int 21h
  149.            jc     ERROR_EXIT
  150.            and dx, 1000h
  151.            jz  ERROR_EXIT
  152.            mov [fResult], 1
  153.            }
  154.         }
  155. ERROR_EXIT:
  156.  
  157. #endif
  158.  
  159.     return fResult;
  160.  
  161. #endif
  162. }
  163.  
  164.  
  165. //----------------------------------------------------------------------------
  166. //   Description:    Determine if a drive is valid
  167. //                          Not valid under unix. Always returns TRUE.
  168. //    Parameters:    sDrive    Drive may be 1..26 (A..Z), A..Z, or a..z.
  169. //                                    Drive is converted to a number 1..26.
  170. //                        psDrive    Drive id converted to a number 1..26
  171. //                                    If null, value is not returned.
  172. //       Returns:    TRUE if drive is valid
  173. //----------------------------------------------------------------------------
  174. BOOL FN_E DriveIsValid(SHORT sDrive, PSHORT psDrive)
  175. {
  176. #if OS_UNIX
  177.  
  178.     NOTUSED(sDrive);
  179.     if (psDrive)
  180.         psDrive[0] = (SHORT)0;
  181.     return TRUE;
  182.  
  183. #else
  184.  
  185.     BOOL fResult;
  186.     SHORT    sOldDrive = DriveGet();
  187.                                                     // Validate drive letter
  188.     if (sDrive == 0)
  189.         sDrive = sOldDrive;
  190.  
  191.     if (sDrive >= 'A' && sDrive <= 'Z')
  192.         sDrive = (SHORT)(sDrive - 'A' + 1);
  193.  
  194.     if (sDrive >= 'a' && sDrive <= 'z')
  195.         sDrive = (SHORT)(sDrive - 'a' + 1);
  196.  
  197.     if (sDrive < 1 || sDrive > 26)
  198.         return FALSE;
  199.  
  200.     if (psDrive)
  201.         *psDrive = sDrive;
  202.  
  203.     fResult = (_chdrive(sDrive) == SUCCESS);
  204.     _chdrive(sOldDrive);
  205.  
  206.     return fResult;                            // Done
  207.  
  208. #endif
  209. }
  210.  
  211.  
  212. //----------------------------------------------------------------------------
  213. //   Description:    Set current drive
  214. //                          This function is meaningless under UNIX.
  215. //    Parameters:    sDrive    Drive may be 1..26 (A..Z), A..Z, or a..z.
  216. //                                    Drive is converted to a number 1..26.
  217. //       Returns:    TRUE if drive is valid
  218. //----------------------------------------------------------------------------
  219. BOOL FN_E DriveSet(SHORT sDrive)
  220. {
  221. #if OS_UNIX
  222.     NOTUSED(sDrive);
  223.     return FALSE;
  224. #else
  225.    if (sDrive >= 'A' && sDrive <= 'Z')
  226.         sDrive = (SHORT)(sDrive - 'A' + 1);
  227.  
  228.     if (sDrive >= 'a' && sDrive <= 'z')
  229.         sDrive = (SHORT)(sDrive - 'a' + 1);
  230.  
  231.    if (sDrive < 1 || sDrive > 26)
  232.         return FALSE;
  233.     DriveSetLogical(sDrive);
  234.     return _chdrive(sDrive) == SUCCESS;
  235. #endif
  236. }
  237.  
  238.  
  239. //----------------------------------------------------------------------------
  240. //   Description:    Set logical drive for current drive.
  241. //    Parameters:    sDrive        Drive letter.
  242. //       Returns:    Previous logical drive.
  243. //----------------------------------------------------------------------------
  244. static SHORT FN_L DriveSetLogical(SHORT sDrive)
  245. {
  246. #if OS_DOS || OS_WINDOWS
  247.     char sCurDrive;                            // Current drive attached to block device. 
  248.     union REGS regs;                           // Registers for interrupt calls.
  249.  
  250.     sCurDrive = (char)DriveGetLogical(sDrive);
  251.     if (sCurDrive != 0 && sCurDrive != sDrive)
  252.           {                                            // Check for DOS version 3.20 or above. 
  253.           if (_osmajor > 3 || _osmajor == 3 && _osminor >= 20)
  254.             {                                        // IOCTL request 0x0F (set logical drive). 
  255.             regs.x.ax = 0x440F;                // Drive number (1 = A, 2 = B, etc) is in BL. 
  256.             regs.h.bl = (UCHAR)sDrive;
  257.             intdos(®s, ®s);
  258.             if (regs.x.cflag)                    // Carry flag set, error in _doserrno. 
  259.               sDrive = 0;
  260.             }
  261. //        else if (sDrive == 1 || sDrive == 2)
  262. //        pokeb(DOS_DATA_SEG, 0x0004, sDrive - 1);
  263.         }
  264.     return sCurDrive;
  265. #else
  266.     return sDrive;
  267. #endif
  268. }
  269.  
  270.  
  271. //----------------------------------------------------------------------------
  272. //   Description:    Get drive type
  273. //    Parameters:    sDrive    Drive may be 1..26 (A..Z), A..Z, or a..z.
  274. //                                    Drive is converted to a number 1..26.
  275. //                        plTotal    If not null, returns total disk size in bytes.
  276. //                        plFree    If not null, returns free disk size in bytes.
  277. //       Returns:    TRUE if successful.
  278. //----------------------------------------------------------------------------
  279. SHORT FN_E DriveSize(SHORT sDrive, PLONG plTotal, PLONG plFree)
  280. {
  281.     LONG lTotal = 0;
  282.     LONG lFree = 0;
  283. #if OS_UNIX==0
  284.     struct diskfree_t dtable;
  285. #endif
  286.  
  287.     if (plTotal)                                // Set initial values
  288.         plTotal[0] = lTotal;
  289.     if (plFree)
  290.         plFree[0] = lFree;
  291.  
  292. #if OS_UNIX
  293.     NOTUSED(sDrive);
  294. #else
  295.     if (!DriveIsValid(sDrive, &sDrive))
  296.         return DRV_OTHER;
  297.     CritErrReset();
  298.     if (_dos_getdiskfree((unsigned)sDrive, &dtable)
  299.     || CritErr())
  300.         {
  301.         Error("Drive %c: not available.", 'A' + sDrive - 1);
  302.         return DRV_OTHER;
  303.         }
  304.  
  305.     lTotal = (long)dtable.sectors_per_cluster * (long)dtable.bytes_per_sector
  306.         * (long)dtable.total_clusters;
  307.     lFree = (long)dtable.sectors_per_cluster * (long)dtable.bytes_per_sector
  308.         * (long)dtable.avail_clusters;
  309.  
  310. #endif
  311.     if (plTotal)
  312.         plTotal[0] = lTotal;
  313.     if (plFree)
  314.         plFree[0] = lFree;
  315.     return TRUE;
  316. }
  317.  
  318. //----------------------------------------------------------------------------
  319. //   Description:    Get drive type
  320. //    Parameters:    sDrive    Drive may be 1..26 (A..Z), A..Z, or a..z.
  321. //                                    Drive is converted to a number 1..26.
  322. //       Returns:    Drive type
  323. //----------------------------------------------------------------------------
  324. SHORT FN_E DriveType(SHORT sDrive)
  325. {
  326. #if OS_UNIX
  327.  
  328.     NOTUSED(sDrive);
  329.     return DRV_OTHER;
  330.  
  331. #elif OS_DOS || OS_WINDOWS
  332.     SHORT sDriveType = DRV_OTHER;
  333.  
  334.     if (!DriveIsValid(sDrive, &sDrive))
  335.         return DRV_OTHER;
  336.  
  337.     if (_osmajor > 3 || _osmajor == 3 && _osminor >= 20)
  338.         {
  339.         BYTE bBuf[0x26];
  340.         PBYTE pb = bBuf;
  341.  
  342.         memset(bBuf, 0, sizeof(bBuf));
  343.         _asm {
  344.             push si
  345.             push di
  346.             push ds
  347.  
  348.             mov ax, 440Dh
  349.             mov bx, [sDrive]
  350.             mov cx, 0860h
  351.             lds dx, [pb]
  352.             mov di, dx                            // OS/2 comp box
  353.             mov si, ds                            // OS/2 comp box
  354.             int 21h
  355.  
  356.             pop ds
  357.             pop di
  358.             pop si
  359.  
  360.             jc    ERROR_EXIT
  361.             }
  362.         sDriveType = (SHORT)bBuf[1];
  363.         }
  364.     if (sDriveType < DRV_FIRST || sDriveType > DRV_LAST)
  365.         sDriveType = DRV_OTHER;
  366. ERROR_EXIT:
  367.     return sDriveType;
  368.  
  369. #else
  370.     NOTUSED(sDrive);
  371.     return DRV_OTHER;
  372. #endif
  373. }
  374.  
  375. //----------------------------------------------------------------------------
  376. //   Description:    Run standard test suite
  377. //    Parameters:    sTest        Test to run.
  378. //                                        0        Run all default tests (except).
  379. //       Returns:    TRUE if successful.
  380. //----------------------------------------------------------------------------
  381. #if COMPILE_TEST
  382. BOOL FN DriveTest(SHORT sTest)
  383. {
  384. #if OS_UNIX == 0
  385.     SHORT sDrive = 0;
  386.     SHORT i;
  387.     LONG lTotal, lFree;
  388.  
  389.     if ((sDrive = DriveGet()) != 0)
  390.         Output("Current drive is %c:\n", sDrive + 'A' - 1);
  391.     else
  392.         Output("Current drive is cannot be determined!\n");
  393.  
  394.     for (i = 'A'; i <= 'Z'; ++i)
  395.         if (DriveIsValid(i, NULL))
  396.             {
  397.             Output("%c: is a valid drive.", i);
  398.             if (DriveIsRemote(i))
  399.                 Output(" Drive is remote.");
  400.             Output(" Drive is ");
  401.             switch (DriveType(i))
  402.                 {
  403.                 case DRV_360:
  404.                     Output("5.25\" 360 Kb.");
  405.                     break;
  406.  
  407.                 case DRV_12:
  408.                     Output("5.25\" 1.2 Mb.");
  409.                     break;
  410.  
  411.                 case DRV_720:
  412.                     Output("3.5\" 720 Kb.");
  413.                     break;
  414.  
  415.                 case DRV_8_SD:
  416.                     Output("8\" SD");
  417.                     break;
  418.  
  419.                 case DRV_8_DD:
  420.                     Output("8\" DD.");
  421.                     break;
  422.  
  423.                 case DRV_FIXED:
  424.                     Output("FIXED.");
  425.                     break;
  426.  
  427.                 case DRV_TAPE:
  428.                     Output("TAPE.");
  429.                     break;
  430.  
  431.                 case DRV_144:
  432.                     Output("3.5\" 1.44 Mb.");
  433.                     break;
  434.  
  435.                 default:
  436.                 case DRV_OTHER:
  437.                     Output("OTHER.");
  438.                     break;
  439.  
  440.                 }
  441.             Output("\n");
  442.             if (DriveSize(i, &lTotal, &lFree))
  443.                 Output("   %ld bytes total, %ld bytes free.\n", lTotal, lFree);
  444.             }
  445. #endif
  446.     NOTUSED(sTest);
  447.     return TRUE;
  448. }
  449. #endif
  450. //----------------------------------------------------------------------------
  451. //------------------------------- End of File --------------------------------
  452. //----------------------------------------------------------------------------
  453.